#!/bin/bash

#Load funtion
. /etc/init.d/functions 

Tools_Dir=/root/tools
Install_Dir=/usr/local/net-snmp
Snmp_Ver=net-snmp-5.7.3

#init
mkdir -p $Tools_Dir



remove_snmp (){
	echo "Remove SNMP rpm "
		line=`rpm -qa|grep snmp|wc -l`
		[ $line -gt 0 ] && {
			rpm -e net-snmp-utils
				rpm -e net-snmp
				rpm -e net-snmp-agent-libs
				rpm -e hpijs libsane-hpaio hplip-libs
				rpm -e net-snmp-libs
				rpm -e postfix
				rpm -e mariadb-libs
				[ $? -eq 0 ] && action "Remove SNMP rpm " /bin/true
		}

}

install_sys_command(){
	echo "Start Install system Command "
		yum install -y wget gcc perl-devel unzip lsof 

}


download_snmp_code(){
	echo "download SNMP...."
		cd $Tools_Dir
		[ -e $Snmp_Ver.zip ] || {
			wget http://120.52.72.22/jaist.dl.sourceforge.net/c3pr90ntc0td/project/net-snmp/net-snmp/5.7.3/$Snmp_Ver.zip
		}
	[ -e $Snmp_Ver.zip ]&& unzip -u $Snmp_Ver.zip >/dev/null 2>&1
}


install_snmp(){

	echo "Install SNMP "

		Snmp_Dir=$Tools_Dir/$Snmp_Ver

		[ -d $Snmp_Dir ]&& {
			cd $Snmp_Dir 
				./configure  \
				--prefix=$Install_Dir \
				--with-default-snmp-version="3" \
				--with-sys-contact="contact"  \
				--with-sys-location="location" \
				--with-persistent-directory="/var/net-snmp" \
				--with-logfile="/var/log/snmpd.log"    && \
				make  &&  make install

				[ $? -eq 0 ] && action "Install SNMP " /bin/true || action "Install SNMP "  /bin/false


		}|| echo " snmp Folder is missing!  Cannot install snmp"

}

configure_snmp(){

	echo "Start Configure SNMP"
#copy ori_config to current set
		mkdir /etc/snmp -p
		/bin/cp $Tools_Dir/$Snmp_Ver/EXAMPLE.conf /etc/snmp/snmpd.conf


#set SNMP Path
		/bin/cp /etc/profile /etc/profile.bak

		count_line=`egrep "^#SNMP PATH" /etc/profile |wc -l `

		if [ $count_line -gt 0 ];then
			action "Write SNMP ENV configure " /bin/false
		else

#wire to ENV
cat  >>/etc/profile<<EOF
#SNMP PATH
Snmp_Path=$Install_Dir
Path=$Snmp_Path/sbin:$PATH
export PATH
EOF

				soure /etc/profile
				action "Write SNMP  ENV configure " /bin/true

				fi

				#bak
				/bin/cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.bak 

				count_line=`egrep "^#grop_id" /etc/snmp/snmpd.conf |wc -l `

				if [ $count_line -gt 0 ];then
					action "Write SNMP configure file" /bin/false
				else
cat >>/etc/snmp/snmpd.conf<<EOF
#group_id

## sec.name source community
com2sec notConfigUser default public

## group.name sec.model sec.name
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser

## incl/excl subtree mask
view systemview included .1

## context sec.model sec.level prefix read write notif
access notConfigGroup "" any noauth exact systemview none none

# system Location
syslocation 

# Added for support of bcm5820 cards.
pass .1.3.6.1.4.1.4413.4.1 /usr/bin/ucd5820stat 
EOF
					action "Write SNMP configure file" /bin/true

					add_systemctl_ser

				fi
}


#Add systemctl serveice
add_systemctl_ser(){
	echo "Start Add SNMP Service "
cat >/lib/systemd/system/snmpd.service<<EOF
[Unit]
Description=Simple Network Management Protocol (SNMP) Daemon.
After=syslog.target network.target
[Service]
Type=forking
PIDFile=/run/net-snmp.pid
ExecStart=$Install_Dir/sbin/snmpd -c /etc/snmp/snmpd.conf -p /run/net-snmp.pid
[Install]
WantedBy=multi-user.target
EOF

		#kill snmp
		pkill snmp
		/usr/bin/systemctl daemon-reload
		/usr/bin/systemctl enable snmpd.service
		/usr/bin/systemctl restart snmpd.service

		count_line=`lsof -i:161 |sed 1d|wc -l`

		if [ $count_line -eq 1 ];then
			action "Add SNMP Service  " /bin/true
		else
			action "Add SNMP Service " /bin/false
				fi

				/usr/bin/systemctl daemon-reload
}

main () {

	remove_snmp
		install_sys_command
		download_snmp_code
		install_snmp
		configure_snmp

}


main
